package com.ruoyi.project.cms.type.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.type.domain.CmsType;
import com.ruoyi.project.cms.type.service.ICmsTypeService;
import com.ruoyi.project.system.post.domain.Post;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/cms/type")
public class CMSTypeController  extends BaseController {

    private String prefix = "cms/type";

    @Autowired
    private ICmsTypeService cmsTypeService;

    @RequiresPermissions("cms:type:view")
    @GetMapping()
    public String operlog()
    {
        return prefix + "/type";
    }

    @RequiresPermissions("cms:type:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CmsType CmsType)
    {
        startPage();
        List<CmsType> list = cmsTypeService.selectPostList(CmsType);
        return getDataTable(list);
    }



    /**
     * 新增公告类别窗口
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存类别窗口
     */
    @RequiresPermissions("cms:type:add")
    @Log(title = "系统公告", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated CmsType cmsType)
    {
        if (UserConstants.CMS_TYPE_NOT_UNIQUE.equals(cmsTypeService.checkTypeNameUnique(cmsType)))
        {
            return error("新增岗位'" + cmsType.getTypeName() + "'失败，公告类别名称已存在");
        }
        return toAjax(cmsTypeService.insertCmsType(cmsType));
    }

    /**
     * 修改岗位
     */
    @GetMapping("/edit/{typeId}")
    public String edit(@PathVariable("typeId") Long typeId, ModelMap mmap)
    {
//        System.out.println(cmsTypeService.selectCmsTypeById(typeId));
        mmap.put("type", cmsTypeService.selectCmsTypeById(typeId));
        return prefix + "/edit";
    }


    /**
     * 修改保存公告类别
     */
    @RequiresPermissions("cms:type:edit")
    @Log(title = "系统公告", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated CmsType cmsType)
    {
        if (UserConstants.CMS_TYPE_NOT_UNIQUE.equals(cmsTypeService.checkTypeNameUnique(cmsType)))
        {
            return error("修改公告类别'" + cmsType.getTypeName() + "'失败，公告类别名称已存在");
        }
        return toAjax(cmsTypeService.updateCmsType(cmsType));
    }

    /** 删除公告类型  */
    @RequiresPermissions("cms:type:remove")
    @Log(title = "系统公告", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax(cmsTypeService.deletePostByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }



    /**
     * 校验公告类型名称
     */
    @PostMapping("/checkTypeNameUnique")
    @ResponseBody
    public String checkPostNameUnique(CmsType cmsType)
    {
        return cmsTypeService.checkTypeNameUnique(cmsType);
    }


}
